package cn.cloud.project.consumer.controller;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.client.naming.NacosNamingService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: cql
 * @date: 2024/11/3
 * @description:
 */
@RestController
@RequestMapping("/nacos-naming")
public class NacosNamingController {

    @Resource
    private NacosNamingService nacosNamingService;

    /**
     * 获取注册到nacos中的所有微服务名
     */
    @GetMapping("/service")
    public ListView<String> getService() {
        try {
            return nacosNamingService.getServicesOfServer(1, Integer.MAX_VALUE);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据serviceName获取所有实例
     */
    @GetMapping("/instance")
    public List<Instance> getAllInstancesByServiceName(String serviceName) {
        try {
            return nacosNamingService.getAllInstances(serviceName);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }


}
